﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPressure
{
    public partial class AddProjectForm : BaseForm
    {
        #region 属性
        /// <summary>项目模型
        /// 
        /// </summary>
        private Project obj = new Project();

        /// <summary>试压模板模型
        /// 
        /// </summary>
        public TemplateItem templateObj;

        #endregion

        #region 自定义方法
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = ProjectNumbernput.Text;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                AntdUI.Message.error(this, where["project_number"] + "工程编号已存在", Font);
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>刷新模板信息
        /// 
        /// </summary>
        public void freshTemplateInfo()
        { 
            DesignPressureInput.Text = templateObj.design_pressure.ToString();
            if (templateObj.pipe_position.Contains("站内/集输"))
            {
                TestTypeSelect.Text = "站内/集输";
            }
            else if (templateObj.pipe_position.Contains("站内"))
            {
                TestTypeSelect.Text = "站内";
            }
            else if (templateObj.pipe_position.Contains("集输"))
            {
                TestTypeSelect.Text = "集输";
            }
        }

        #endregion

        #region 系统方法
        public AddProjectForm()
        {
            InitializeComponent();
        }
        #endregion


        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (ProjectNumbernput.Text.Length == 0)
            {
                Alert.Error("请输入工程编号", this);
                ProjectNumbernput.Focus();
                return;
            }
            if (ProjectNameInput.Text.Length == 0)
            {
                Alert.Error("请输入工程名称", this);
                ProjectNameInput.Focus();
                return;
            }
            if (PipeLengthInput.Text.Length == 0)
            {
                Alert.Error("请输入管道长度", this);
                PipeLengthInput.Focus();
                return;
            }
            if (templateObj.template_number <= 0)
            {
                Alert.Error("请选择试压模板", this);
                return;
            }

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }

            obj.project_number = ProjectNumbernput.Text;
            obj.project_name = ProjectNameInput.Text;
            obj.stake_mark = StakeMarkInput.Text;
            obj.pipe_specification = PipeSpecificationInput.Text;
            obj.pipe_length = float.Parse(PipeLengthInput.Text);
            obj.medium = MediumInput.Text;
            obj.design_pressure = float.Parse(DesignPressureInput.Text);
            obj.pipe_nature = PipeNatureSelect.Text;
            obj.test_type = TestTypeSelect.Text;
            obj.company = CompanyInput.Text;
            obj.manometer_precision = ManometerPrecisionSelect.Text;
            obj.manometer_range = ManometerRangeSelect.Text;
            obj.template_id = templateObj.template_number;

            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                this.Close();
                Alert.Info("添加工程成功", this.Owner);
            }
        }

        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            
        }
    }
}
